package com.karry.yl.web.personnel;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.commons.vo.PermissionTreeNode;
import com.karry.yl.domain.baseinfo.Employee;
import com.karry.yl.domain.baseinfo.Organization;
import com.karry.yl.domain.elder.Elderly;
import com.karry.yl.domain.personnel.Department;
import com.karry.yl.domain.personnel.DepartmentTreeTemplate;
import com.karry.yl.service.baseinfo.EmployeeService;
import com.karry.yl.service.baseinfo.OrganizationService;
import com.karry.yl.service.elder.ElderlyService;
import com.karry.yl.service.personnel.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;


/**
 * @author Administrator
 */
@Controller
@RequestMapping("/department")
public class DepartmentController extends SuperController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping
    public String listUI() {
        return "personnel/department/list";
    }

    @RequestMapping("/add")
    public String addUI(Long id, ModelMap map) {
        map.put("id", id);
        return "personnel/department/add";
    }

    @RequestMapping("edit")
    public String editUI(Long id, ModelMap map) {
        Department department = departmentService.selectById(id);
        map.put("department", department);
        return "personnel/department/edit";
    }


    @RequestMapping("/save")
    @ResponseBody
    public JsonResponse save(Long parentId, String name, Integer isNursingDepartment) {
        try {
            Department department = new Department();
            department.setParentId(parentId);
            department.setName(name);
            department.setNursingDepartment(isNursingDepartment == 1 ? true : false);
            department.setOrgId(currentUser.getOrgId());
            departmentService.insert(department);
            return new JsonResponse(200, "创建部门成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResponse(0, "创建部门失败");
    }

    @RequestMapping("/update")
    public String update(Department department) {
        department.setOrgId(currentUser.getOrgId());
        departmentService.updateById(department);
        return "redirect:/department";
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    @ResponseBody
    public JsonResponse delete(Long id) {
        try {
            Department department = departmentService.selectById(id);
            departmentService.deleteById(id);
            departmentService.delete(new EntityWrapper<Department>().eq("orgId", currentUser.getOrgId()).eq("parentId", id).eq("deleted", 0));
            return new JsonResponse(200, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new JsonResponse(0, "操作失败");
    }


    /**
     * 获取部门树数据
     *
     * @return
     */
    @RequestMapping("/tree")
    @ResponseBody
    public JsonResponse permissionTree() {
        try {
            Long orgId = currentUser.getOrgId();
            Organization organization = organizationService.selectById(orgId);
            List<DepartmentTreeTemplate> departments = departmentService.getTree(orgId);
            departments.add(new DepartmentTreeTemplate(organization.getName(), 0L, 0L, true));
            return JsonResponse.success("加载成功", departments);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResponse.fail("加载失败");
    }

    @Autowired
    private ElderlyService elderlyService;
    /**
     * 查看该部门下是否存在绑定的从业人员,如果是护理部查看是否包含老人
     *
     * @return
     */
    @RequestMapping("/containEmployee")
    @ResponseBody
    public JsonResponse containEmployee(Long id) {
        Department department = departmentService.selectById(id);
        List<Employee> employees = employeeService.selectList(new EntityWrapper<Employee>().eq("departmentId", id).eq("orgId", currentUser.getOrgId()).eq("deleted", false));
        //是护理部
        JsonResponse jsonResponse = new JsonResponse();
        if(employees!=null&&employees.size()>0){
            jsonResponse= new JsonResponse(200,"该部门下存在从业人员，如要删除请先修改该部门下的从业人员的部门信息");
        }else {
            if(department.getNursingDepartment()){
                List<Elderly> elderlies = elderlyService.selectList(new EntityWrapper<Elderly>().eq("allocationStart", true));
                if(elderlies!=null&&elderlies.size()>0){
                    jsonResponse=new JsonResponse(200,"该部门类型为护理部，该部门下包含老人，如要删除请先从新分配老人");
                }else {
                    jsonResponse= new JsonResponse(0,"该护理部下不包含老人");
                }
            }
        }
        return jsonResponse;
    }

    /**
     *根据部门的id查询部门
     *
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public JsonResponse findById(Long id) {
        Department department = departmentService.selectById(id);
        return  new JsonResponse(200,"成功",department);
    }
}
